﻿using System.ComponentModel;

namespace VisionCloud.Domain.Common
{
    public enum AjaxCode
    {
        [Description("success")]
        Success = 0,

        [Description("参数错误")]
        ParamError = 101,

        [Description("验证码错误")]
        CaptchaError = 103,

        [Description("登录错误")]
        LoginError = 105,

        [Description("操作错误")]
        Error = 1,

        [Description("操作失败")]
        Fail = 2,

        [Description("重复操作")]
        Exsit = 3,

        [Description("服务端出错啦")]
        GlobalError = 500,

        [Description("自定义异常")]
        CustomError = 110,

        [Description("非法请求")]
        InvalIdRequest = 116,

        [Description("授权失败")]
        OauthFail = 201,

        [Description("未授权")]
        Unauthorized = 401,

        [Description("授权访问失败")]
        Forbidden = 403,

        [Description("Bad Request")]
        BadRequest = 400,

        [Description("Change Password")]
        ChangePassword = 5405,
    }

    public class ApiResult
    {
        public ApiResult()
        {
            Code = AjaxCode.Error;
        }

        public ApiResult(AjaxCode code)
        {
            Code = code;
        }

        public ApiResult(AjaxCode code, string message)
        {
            Code = code;
            Message = message;
        }

        public bool IsSuccess()
        {
            return AjaxCode.Success.Equals(Code);
        }

        public AjaxCode Code { get; set; }
        public string Message { get; set; }

        public bool HasError
        {
            get
            {
                if (Code == AjaxCode.Error || Code == AjaxCode.Fail)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public object Result { get; set; }

        public static ApiResult Error(string msg = "")
        {
            return new ApiResult() { Message = msg };
        }

        public static ApiResult Success(string msg = "")
        {
            return new ApiResult() { Code = AjaxCode.Success, Message = msg };
        }

        public static ApiResult SetCode(AjaxCode ajaxCode, string msg = "")
        {
            return new ApiResult() { Code = ajaxCode, Message = msg };
        }

        public ApiResult SetData(object obj)
        {
            this.Result = obj;
            return this;
        }
    }

    public class ApiResultBase<T>
    {
        /// <summary>
        /// 错误码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 返回内容
        /// </summary>
        public T Result { get; set; }
    }

    public class ApiRespBase<T>
    {
        public int Code { get; set; }

        public string Message { get; set; }

        public T Data { get; set; }
    }
}